package com.changgou.search.service.impl;

import com.changgou.search.dao.CollectMapper;
import com.changgou.search.pojo.Collect;
import com.changgou.search.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Override
    public void collect(String skuId, String username) {
        Collect collect = new Collect();
        collect.setUsername(username);
        collect.setSkuId(skuId);
        int count = collectMapper.selectCount(collect);
        //一个用户只能收藏一次同样的商品
        if (count > 0) {
            throw new RuntimeException("商品已添加收藏,请勿重复添加");
        }
        //添加收藏
        collect.setCreateTime(new Date());
        collectMapper.insertSelective(collect);
    }
}
